package com.swt.gap.controller;

import com.swt.gap.common.exception.ExpiredException;
import com.swt.gap.common.exception.InvalidPasswordException;
import com.swt.gap.model.dto.AchievementShareDTO;
import com.swt.gap.model.entity.AchievementShare;
import com.swt.gap.model.vo.achievementcenter.AchievementShareInfoVO;
import com.swt.gap.service.AchievementShareService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * @author sx
 */
@Api(tags = "503. 战果分享相关")
@RestController
@RequestMapping("/achievement-share")
@Slf4j
public class AchievementShareController {

    private final AchievementShareService achievementShareService;

    public AchievementShareController(AchievementShareService achievementShareService) {
        this.achievementShareService = achievementShareService;
    }

    @PostMapping
    @ApiOperation(value = "保存分享的战果", notes = "保存分享的战果")
    @ApiImplicitParam(name = "achievementShareDTO", dataType = "AchievementShareDTO",paramType = "body" )
    @ApiResponse(code = 201, message = "created")
    public ResponseEntity<AchievementShare> saveAchievementShare(@RequestBody AchievementShareDTO achievementShareDTO){

        return ResponseEntity.status(HttpStatus.CREATED)
                .body(achievementShareService.saveAchievementShare(achievementShareDTO));
    }

    @GetMapping("/{achievementShareId}")
    @ApiOperation(value = "获取分享的战果", notes = "获取分享的战果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "achievementShareId", dataType = "Integer",paramType = "path"),
            @ApiImplicitParam(name = "password", dataType = "String", paramType = "query" )
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = AchievementShare.class),
            @ApiResponse(code = 403, message = "密码错误"),
            @ApiResponse(code = 440, message = "超时")
    })
    public ResponseEntity<AchievementShare> getAchievementShare(@PathVariable Integer achievementShareId,
                                                      @RequestParam(required = false) String password){
        try {
            return ResponseEntity.ok(achievementShareService.getAchievementShare(achievementShareId, password));
        } catch (InvalidPasswordException e) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        } catch (ExpiredException e) {
            return ResponseEntity.status(440).build();
        }
    }


    @GetMapping("/info")
    @ApiOperation(value = "根据战果id 获取分享的基本信息", notes = "根据战果id 获取分享的基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "achievementId", dataType = "Integer",paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = AchievementShareInfoVO.class)
    })
    public ResponseEntity<AchievementShareInfoVO> getAchievementShareInfo(@RequestParam Integer achievementId){
        return ResponseEntity.ok(achievementShareService.getAchievementShareInfo(achievementId));
    }
}

